var api = require('../../utils/api.js');
var app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectedDate:'',
        hid:'',
        sid:'',
        see:0,
        wname: '',
        wid:'',
        dname:'',
        did:'',
        jjmobile:'',
        jjname:'',
        desc:'',
        progressSteps: [
            { label: '1', text: '提交信息', active: true, done: true },
            { label: '2', text: '在线支付', active: false, done: false },
            { label: '3', text: '专人服务', active: false, done: false },
            { label: '4', text: '服务完成', active: false, done: false }
        ],
        selectedDateLabel: '',
        dateRange: [],
        dateValue: [0, 0, 0]
    },
    onDateChange(e) {
        const value = e.detail.value;

        var k1=value[0];
        var k2=value[1];
        var list=this.data.dateRange;
        console.log(value)
        
        console.log(list[0][k1].label)
        console.log(list[1][k2].label)

        var selected=list[0][k1].label +" "+ list[1][k2].label;
        this.setData({
            selectedDate:list[0][k1].label,
            selectedDateLabel: selected,
            wid:'',
            wname:''
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        var id=options.id; //医院id
        var sid=options.sid; //服务id
        var that=this;

        this.setData({
            hid:id,
            sid:sid
        })
     

        
        api.post({
            url: 'index/getdate',
            data: {},
            success: data => {
                console.log(data)
                that.setData({
                    dateRange:data.data
                })
            }
        })

        api.post({
            url: 'hospital/gethospitalbyid',
            data: {id:id},
            success: data => {
                console.log(data)
                that.setData({
                    hospital:data.data
                })
            }
        })

        api.post({
            url: 'hospital/getserverbyid',
            data: {id:sid},
            success: data => {
                console.log(data)
                that.setData({
                    server:data.data
                })
            }
        })
    },


    selectserver(){
        wx.navigateTo({
          url: '/pages/my/fuwu',
        })
    },

    selectwork(){

        var selectedDate=this.data.selectedDate;

        console.log(selectedDate);
        
        if (!selectedDate){
            wx.showToast({
              title: '请选择日期',
              icon:'none'
            })
            return false;
        }

        wx.navigateTo({
            url: '/pages/server/index?selectedDate='+selectedDate
          })
    },
    getjjnamme(ret){
        this.setData({
            jjname:ret.detail.value
        })
    },
    getjjmobile(ret){
        this.setData({
            jjmobile:ret.detail.value
        })
    },
    getdesc(ret){
        this.setData({
            desc:ret.detail.value
        })
    },
    setsee(){
        var see=!this.data.see;

        this.setData({
            see:see
        })

    },
    seexieyi(){
        wx.navigateTo({
          url: '/pages/xieyi/index?id=1',
        })
    },

    //提交信息    
    
    submits(){
        var info={};
        var that=this;
        info.desc=this.data.desc;
        info.jjname=this.data.jjname;
        info.jjmobile=this.data.jjmobile;
        info.did=wx.getStorageSync('did'); //就诊
        info.wid=wx.getStorageSync('wid'); //陪护

        console.log(info);

        info.hid=this.data.hid; //医院
        info.sid=this.data.sid; //服务
        info.dates=this.data.selectedDateLabel; //时间
    
 
        var see=this.data.see;
        if (!see){
            wx.showToast({
              title: '请阅读并同意协议',
              icon:'none'
            })
            return false;
        }

        api.post({
            url: 'order/applyorder',
            data: info,
            success: data => {
                console.log(data);

                if(data.code!=1){

                    wx.showToast({
                        title: data.msg,
                        icon:'none'
                      })
                    return false;
                }
                var orderid=data.data;

                that.payorder(orderid);
               
            }
        })
    },

    payorder(id) {
        var that = this;
        // 支付
        api.post({
            url: 'order/paybyid',
            data: {
                id: id
            },
            success: data => {
                console.log(data);
                wx.requestPayment({
                    timeStamp: data.data.timeStamp,
                    nonceStr: data.data.nonceStr,
                    package: data.data.package,
                    signType: 'MD5',
                    paySign: data.data.paySign,
                    success(res) {
                            wx.reLaunch({
                            url: '/pages/order/index',
                        })
                    },
                    fail(){
                        wx.reLaunch({
                            url: '/pages/order/index',
                        })
                    }
                })
            }
        })
    },


    onShow(option){
        
        console.log(option);
          
    },
    seecinfo(ret){
        console.log(ret);
        var id=ret.currentTarget.dataset.id;

        wx.navigateTo({
          url: '/pages/comment/info?sid='+id
        })
    }

})